package org.calzz.example.crud;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

@Controller("products")
public class ProductController {

	private ProductDAO productsDAO;

	public ProductDTO findById(int productId) {
		return productsDAO.getProductById(productId);
	}

	public List<ProductDTO> find(@RequestParam(required=false) String pattern) {
		if (pattern == null) {
			pattern = "*";
		}

		return productsDAO.findProducts(pattern);
	}

	public void addProduct(ProductDTO product) {
		productsDAO.addProduct(product);
	}

	public void deleteProduct(int productId) {
		productsDAO.deleteProduct(productId);
	}

	public void updateProduct(ProductDTO product) {
		productsDAO.updateProduct(product);
	}

	@Autowired(required=true)
	public void setProductDAO(ProductDAO productDAO) {
		this.productsDAO = productDAO;
	}

}
